package com.zone.domain.strategy.model.entity;

import cn.hutool.core.bean.BeanUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.zone.types.exception.BizException;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author: zongzi
 * @description: 策略规则
 * @date: 2024/5/20
 */
@Data
public class StrategyRuleEntity {
    /** 抽奖策略ID */
    private Long strategyId;
    /** 抽奖奖品ID【规则类型为策略，则不需要奖品ID】 */
    private Integer awardId;
    /** 抽象规则类型；1-策略规则、2-奖品规则 */
    private Byte ruleType;
    /** 抽奖规则类型【rule_random - 随机值计算、rule_lock - 抽奖几次后解锁、rule_luck_award - 幸运奖(兜底奖品)】 */
    private String ruleModel;
    /** 抽奖规则比值 */
    private String ruleValue;
    /** 抽奖规则描述 */
    private String ruleDesc;

    // 解析权重策略配置
    // 4000:102,103,104,105;5000:102,103,104,105,106,107;6000:102,103,104,105,106,107,108,109
    public Map<String, List<Integer>> parseRuleWeightValues() {
        Map<String, List<Integer>> result = Maps.newTreeMap();
        if (Strings.isEmpty(ruleValue)) {
            return result;
        }
        // 用户积分 - 奖品集合
        String[] userScoreAwardValues = StringUtils.split(ruleValue, ";");
        for (String userScoreAwardValue : userScoreAwardValues) {
            // 用户积分权重
            String ruleWeight = StringUtils.substringBefore(userScoreAwardValue, ":");
            // 奖品集合字符串
            String ruleWeightValueStr = StringUtils.substringAfter(userScoreAwardValue, ":");
            if (Strings.isEmpty(ruleWeight) || Strings.isEmpty(ruleWeightValueStr)) {
                throw new IllegalArgumentException(String.format("rule weight value illegal: %s", userScoreAwardValue));
            }
            String[] ruleWeightValues = StringUtils.split(ruleWeightValueStr, ",");
            result.put(ruleWeight, BeanUtil.copyToList(Arrays.asList(ruleWeightValues), Integer.class));
        }
        return result;
    }


    /**
     * 解析黑名单配置值（101:4001,4002,4003）
     *
     * @return Map<Integer, List < String>> key: awardId, value: userIdList
     */
    public Map<Integer, List<String>> parseBlacklistRuleValues() {
        Map<Integer, List<String>> resultTable = Maps.newTreeMap();
        if (Strings.isEmpty(ruleValue)) {
            return resultTable;
        }
        String awardId = StringUtils.substringBefore(ruleValue, ":");
        if (Strings.isEmpty(awardId)) {
            throw new BizException(String.format("blacklist rule value award is null: %s", ruleValue));
        }
        String userIds = StringUtils.substringAfter(ruleValue, ":");
        // 黑名单配置用户id为null
        if (Strings.isEmpty(userIds)) {
            resultTable.put(Integer.valueOf(awardId), Lists.newArrayList());
            return resultTable;
        }
        String[] userIdArray = StringUtils.split(userIds, ",");
        resultTable.put(Integer.valueOf(awardId), Arrays.asList(userIdArray));
        return resultTable;
    }
}
